import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:robot3/services.dart';
import 'package:robot3/widgets.dart';
import '../backButton1.dart';
import '../viewmodel/add_qa_provider.dart';
import '../viewmodel/map_provider.dart';
import '../viewmodel/product_provider.dart';
import 'map_widget.dart';

//户型选择
void main() {
  runApp(MaterialApp(home: const AddQAScreen()));
}

class AddQAScreen extends StatelessWidget {
  const AddQAScreen({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => AddQAProvider()),
        ChangeNotifierProvider(create: (_) => MapProvider()),
        ChangeNotifierProvider(create: (_) => ProductProvider()),
      ],
      child: _ShangpinkuScreen(title: 'Flutter Demo Home Page'),
    );
  }
}

class _ShangpinkuScreen extends StatefulWidget {
  const _ShangpinkuScreen({super.key, required this.title});

  final String title;

  @override
  State<_ShangpinkuScreen> createState() => _PageState();
}

class _PageState extends State<_ShangpinkuScreen> {
  @override
  void initState() {
    super.initState();
    print("fetchData");
    final mapProvider = Provider.of<MapProvider>(context, listen: false);
    mapProvider.fetchAllMaps();

    // provider.login().then((value) {
    //   if (value) {
    //     provider.getSysDicByType(
    //       accessToken: Robot.accessToken,
    //       type: "HOUSE_TYPE",
    //     );
    //     provider
    //         .getHouseLayouts(
    //           accessToken: Robot.accessToken,
    //           userId: Robot.userId,
    //           companyId: Robot.companyId,
    //         )
    //         .then((value) {});
    //   }
    // });
  }

  @override
  Widget build(BuildContext context) {
    return RobotScreen(child: Content01(child: Content()));
  }
}

class Content01 extends StatelessWidget {
  final Widget child;

  const Content01({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 260 / ratio),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "新增语音配置",
            style: TextStyle(
              color: MyBlack,
              fontSize: 30 / ratio,
              fontWeight: FontWeight.bold,
            ),
          ),
          SizedBox(height: 30 / ratio),
          Expanded(child: child),
        ],
      ),
    );
  }
}

class Info extends StatefulWidget {
  const Info({super.key});

  @override
  _InfoState createState() => _InfoState();
}

class _InfoState extends State<Info> {
  final TextEditingController phoneController = TextEditingController();
  final TextEditingController nameController = TextEditingController();
  final TextEditingController nameController2 = TextEditingController();

  @override
  void dispose() {
    // Always dispose of controllers when the widget is disposed
    phoneController.dispose();
    nameController.dispose();
    nameController2.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final TextEditingController controller = TextEditingController();
    final map = context.watch<AddQAProvider>().map;
    return Consumer<AddQAProvider>(
      builder: (context, provider, child) {
        return Padding(
          padding: EdgeInsets.all(0),
          child: Column(
            spacing: 30 / ratio,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Text(
                    "        类型",
                    style: TextStyle(
                      color: MyBlack,
                      fontSize: 24 / ratio,
                      fontWeight: FontWeight.normal,
                    ),
                  ),
                  SizedBox(width: 20 / ratio),
                  SizedBox(
                    width: 374 / ratio,
                    height: 44 / ratio,
                    child: GestureDetector(
                      onTap: () {
                        print("1");
                      },
                      child: RobotPopupMenuButton2(
                        init: provider.selectedType,
                        map: map,
                        width: 374 / ratio,
                        height: 44 / ratio,
                        itemHeight: 80 / ratio,
                        offsetY: 44 / ratio,
                        onItemSelected: (type) {
                          provider.setSelectedType(type);
                          print(type);
                        },
                      ),
                    ),
                  ),
                ],
              ),
              buildContent(provider.selectedType, controller),
              SizedBox(width: 47 / ratio),
              Row(
                children: [
                  SizedBox(width: 98 / ratio),
                  ConfirmButton(
                    onClick: (text) {
                      print(phoneController.text);
                      print(nameController.text);
                    },
                    text: "确认",
                    width: 148 / ratio,
                  ),
                ],
              ),
            ],
          ),
        );
      },
    );
  }
}

Widget buildContent(String type, TextEditingController controller) {
  return Consumer<AddQAProvider>(
    // Listen to changes in AddQAProvider
    builder: (context, provider, child) {
      switch (type) {
        case "机器人招揽":
          return WakeupWidget();
        case "商品介绍":
          return ProductWidget();
        case "自定义问答":
          return QAWidget();
        case "地点引领播报":
          return MapWidget();
          // return Row(children: [Text("地点引领播报"), Icon(Icons.map)]);
        case "操作指令":
          return Column(
            children: [
              Text("操作指令"),
              ElevatedButton(
                onPressed: () {
                  print("操作执行");
                },
                child: Text("执行"),
              ),
            ],
          );
        default:
          return Text("未定义类型");
      }
    },
  );
}

class Content extends StatelessWidget {
  const Content({super.key});

  @override
  Widget build(BuildContext context) {
    return RobotCard(
      color: Colors.white,
      child: Padding(
        padding: EdgeInsets.all(30 / ratio),
        child: SizedBox(
          child: ClipRRect(
            borderRadius: BorderRadius.circular(20.0 / ratio),
            // 保持 CustomScrollView 圆角一致
            child: CustomScrollView(
              physics: BouncingScrollPhysics(),
              slivers: [SliverToBoxAdapter(child: Info())],
            ),
          ),
        ),
      ),
    );
  }
}

class QAWidget extends StatelessWidget {
  const QAWidget({super.key});
  @override
  Widget build(BuildContext context) {
    return Consumer<AddQAProvider>(
      // Listen to changes in AddQAProvider
      builder: (context, provider, child) {
        return Column(
          spacing: 30 / ratio,
          children: [
            Row(
              children: [
                Text(
                  "        标题",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                SizedBox(
                  width: 374 / ratio,
                  height: 44 / ratio,
                  child: RobotEditText2(
                    init: provider.qaTitle,
                    onTextChanged: (text) {

                    },
                  ),
                ),
              ],
            ),

            Row(
              children: [
                Text(
                  "对应内容",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                SizedBox(
                  width: 374 / ratio,
                  height: 44 / ratio,
                  child: RobotEditText2(
                    init: provider.qaTitle2,
                    onTextChanged: (text) {},
                  ),
                ),
              ],
            ),

            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "    关键词",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                Column(
                  spacing: 20 / ratio,
                  children: List.generate(provider.contents.length, (index) {
                    final controller = TextEditingController(
                      text: provider.contents[index],
                    );

                    // Add a listener to the controller
                    controller.addListener(() {
                      // This is triggered every time the text changes
                      print("Text changed to: ${controller.text}");
                      provider.updateContent(index, controller.text);
                    });
                    return  Row(children: [
                      SizedBox(
                      width: 374 / ratio,
                      height: 44 / ratio,
                      child: RobotEditText(
                        controller: controller,
                        onTextChanged: (text) {},
                      ),
                    ),

                   // if(index!=0)
                     SizedBox(width: 42 / ratio),
                      // if(index!=0)
                        Ink(
                    width: 40 / ratio,
                    height: 40 / ratio,
                    decoration: const ShapeDecoration(
                    color: Colors.black12, // A constant color value
                    shape: CircleBorder(),
                    ),
                    child: SizedBox(
                    width: 40 / ratio,
                    height: 40 / ratio,
                    child: IconButton(
                    padding: EdgeInsets.zero,
                    iconSize: 40 / ratio,
                    icon: Icon(index==0 ? Icons.add :Icons.remove),
                    color: Colors.black,
                    onPressed: () {
                      if(index==0) {
                        provider.addSize();
                      } else
                        {
                          provider.removeAt(index);
                        }
                    },
                    ),
                    ),
                    ),
                    ]
                    );
                  }),
                ),
              ],
            ),
          ],
        );
      },
    );
  }
}





class ProductWidget extends StatelessWidget {
  const ProductWidget({super.key});
  @override
  Widget build(BuildContext context) {
    return Consumer<ProductProvider>(
      // Listen to changes in AddQAProvider
      builder: (context, provider, child) {
        return Column(
          spacing: 30 / ratio,
          children: [
            Row(
              children: [
                Text(
                  "        标题",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                SizedBox(
                  width: 374 / ratio,
                  height: 44 / ratio,
                  child: RobotEditText2(
                    init: provider.qaTitle,
                    onTextChanged: (text) {},
                  ),
                ),
              ],
            ),

            Row(
              children: [
                Text(
                  "商品介绍",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                SizedBox(
                  width: 374 / ratio,
                  height: 44 / ratio,
                  child: RobotEditText2(
                    init: provider.qaTitle2,
                    onTextChanged: (text) {},
                  ),
                ),
              ],
            ),

            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "    关键词",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                Column(
                  spacing: 20 / ratio,
                  children: List.generate(provider.keyowrds.length, (index) {
                    final controller = TextEditingController(
                      text: provider.keyowrds[index],
                    );

                    // Add a listener to the controller
                    controller.addListener(() {
                      // This is triggered every time the text changes
                      print("Text changed to: ${controller.text}");
                      provider.updateContent(index, controller.text);
                    });
                    return  Row(children: [
                      SizedBox(
                        width: 374 / ratio,
                        height: 44 / ratio,
                        child: RobotEditText(
                          controller: controller,
                          onTextChanged: (text) {},
                        ),
                      ),

                      // if(index!=0)
                      SizedBox(width: 42 / ratio),
                      // if(index!=0)
                      Ink(
                        width: 40 / ratio,
                        height: 40 / ratio,
                        decoration: const ShapeDecoration(
                          color: Colors.black12, // A constant color value
                          shape: CircleBorder(),
                        ),
                        child: SizedBox(
                          width: 40 / ratio,
                          height: 40 / ratio,
                          child: IconButton(
                            padding: EdgeInsets.zero,
                            iconSize: 40 / ratio,
                            icon: Icon(index==0 ? Icons.add :Icons.remove),
                            color: Colors.black,
                            onPressed: () {
                              if(index==0) {
                                provider.addSize();
                              } else
                              {
                                provider.removeAt(index);
                              }
                            },
                          ),
                        ),
                      ),
                    ]
                    );
                  }),
                ),
              ],
            ),
          ],
        );
      },
    );
  }
}

class WakeupWidget extends StatelessWidget {
  const WakeupWidget({super.key});
  @override
  Widget build(BuildContext context) {
    return Consumer<AddQAProvider>(
      // Listen to changes in AddQAProvider
      builder: (context, provider, child) {
        return Column(
          spacing: 30 / ratio,
          children: [
            Row(
              children: [
                Text(
                  "        标题",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                SizedBox(
                  width: 374 / ratio,
                  height: 44 / ratio,
                  child: RobotEditText2(
                    init: provider.qaTitle,
                    onTextChanged: (text) {},
                  ),
                ),
              ],
            ),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "    招揽语",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                Column(
                  spacing: 20 / ratio,
                  children: List.generate(provider.contents.length, (index) {
                    final controller = TextEditingController(
                      text: provider.contents[index],
                    );

                    // Add a listener to the controller
                    controller.addListener(() {
                      // This is triggered every time the text changes
                      print("Text changed to: ${controller.text}");
                      provider.updateContent(index, controller.text);
                    });
                    return  Row(children: [
                      SizedBox(
                        width: 374 / ratio,
                        height: 44 / ratio,
                        child: RobotEditText(
                          controller: controller,
                          onTextChanged: (text) {},
                        ),
                      ),

                      // if(index!=0)
                      SizedBox(width: 42 / ratio),
                      // if(index!=0)
                      Ink(
                        width: 40 / ratio,
                        height: 40 / ratio,
                        decoration: const ShapeDecoration(
                          color: Colors.black12, // A constant color value
                          shape: CircleBorder(),
                        ),
                        child: SizedBox(
                          width: 40 / ratio,
                          height: 40 / ratio,
                          child: IconButton(
                            padding: EdgeInsets.zero,
                            iconSize: 40 / ratio,
                            icon: Icon(index==0 ? Icons.add :Icons.remove),
                            color: Colors.black,
                            onPressed: () {
                              if(index==0) {
                                provider.addSize();
                              } else
                              {
                                provider.removeAt(index);
                              }
                            },
                          ),
                        ),
                      ),
                    ]
                    );
                  }),
                ),
              ],
            ),
          ],
        );
      },
    );
  }
}

